package vn.edu.hcmut.cse.ai.search.problems.npuzzle;

import vn.edu.hcmut.cse.ai.search.local.HeuristicFunction;

public class NPuzzleSecondHeuristicFunction implements HeuristicFunction{

    /**
     *  Calculate the distance between the current state and the goal stating 
     * by counting the different cells.
     * @param currentState  The current state
     * @param goalState The goal state
     * @return  The distance between the current state and the goal state
     */
    @Override
    public double heuristicEvaluation(Object currentState, Object goalState) {
        NPuzzleState nPuzzleCurrentState = (NPuzzleState) currentState;
        NPuzzleState nPuzzleGoalState = (NPuzzleState) goalState;
        
        int[][] currentBoard = nPuzzleCurrentState.getBoard();
        int[][] goalBoard = nPuzzleGoalState.getBoard();
        int size = nPuzzleCurrentState.getSize();
        boolean exit;
        int distancePoint = 0;
        for (int i = 0; i < size; i ++) {
            for (int j = 0; j < size; j ++) {
                exit=false;
                if (currentBoard[i][j] != goalBoard[i][j]) {
                    for (int m=0;m<size;m++){
                        for (int n = 0; n<size; n++){
                            if (currentBoard[i][j]==goalBoard[m][n]){
                                distancePoint+=(Math.abs(i-m)+Math.abs(j-n));
                                exit = true;
                                break;
                            }
                        }
                        if (exit) break;
                    }
                }
            }
        }
        return (double) distancePoint;
    }
    
}
